import { createRequestTypes } from '@UTILS/reduxHelper'
import {
  AuthActionITF,
  AuthLoginPayLoad,
  AuthLoginSuccessActionITF,
  AuthRequestAuthCodePayLoad,
  AuthVisitorLoginPayLoad
} from '@REDUXITF/auth'
import { UserITF, WeChatUserInfoITF } from '@INTERFACES/user'
import { Action } from 'redux'

export const LOGIN_WEIXIN = createRequestTypes('LOGIN_WEIXIN')
export const REQUEST_AUTH_CODE = createRequestTypes('REQUEST_AUTH_CODE')
export const LOGIN_OR_REGISTER_BY_PHONE = createRequestTypes('LOGIN_OR_REGISTER_BY_PHONE')
export const LOGOUT_USER = createRequestTypes('LOGOUT_USER')
export const VISITOR_LOGIN = createRequestTypes('VISITOR_LOGIN')
export const WECHAT_LOGIN = createRequestTypes('WECHAT_LOGIN')
export const CHECK_STATUS = createRequestTypes('CHECK_STATUS')
export const UPDATE_NETWORK_STATUS = createRequestTypes('UPDATE_NETWORK_STATUS')
export const SET_LOGIN_SUCCESS_INFO = createRequestTypes('SET_LOGIN_SUCCESS_INFO')
export const GET_SYSTEM_CONFIG = createRequestTypes('GET_SYSTEM_CONFIG')

export default {
  dispatchLoginWeiXin(payload: AuthLoginPayLoad): AuthActionITF {
    return {
      type: LOGIN_WEIXIN.REQUEST,
      payload
    }
  },
  dispatchRequestAuthCode(payload: AuthRequestAuthCodePayLoad) : AuthActionITF {
    return {
      type: REQUEST_AUTH_CODE.REQUEST,
      payload
    }
  },
  dispatchLoginOrRegisterByPhone(payload: AuthLoginPayLoad): AuthActionITF {
    return {
      type: LOGIN_OR_REGISTER_BY_PHONE.REQUEST,
      payload
    }
  },
  dispatchLogout(disableLogin: boolean): AuthActionITF {
    return {
      type: LOGOUT_USER.REQUEST,
      disableLogin
    }
  },
  dispatchVisitorLogin(payload: AuthVisitorLoginPayLoad, visitorLoginSuccess?: (userInfo : UserITF) => void): AuthActionITF {
    return {
      type: VISITOR_LOGIN.REQUEST,
      payload,
      visitorLoginSuccess
    }
  },
  dispatchNetWorkStatus(isConnected: boolean) : AuthActionITF {
    return {
      type: UPDATE_NETWORK_STATUS.REQUEST,
      isConnected
    }
  },
  dispatchWeChatLogin(payload: WeChatUserInfoITF) : AuthActionITF {
    return {
      type: WECHAT_LOGIN.REQUEST,
      payload,
    }
  },
  dispatchCheckStatus() : Action {
    return {
      type: CHECK_STATUS.REQUEST,
    }
  },
  dispatchSetLoginSuccessInfo(loginSuccess: () => void) : AuthLoginSuccessActionITF {
    return {
      type: SET_LOGIN_SUCCESS_INFO.REQUEST,
      loginSuccess,
    }
  },
  dispatchGetSystemConfig() : Action {
    return {
      type: GET_SYSTEM_CONFIG.REQUEST,
    }
  },
}
